<?php


namespace App\Http\Controllers\App\V3;


use App\Enums\RedisKeyEnum;
use App\Enums\RoomEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\PushService;
use Illuminate\Http\Request;

class BackGroundController extends BaseController
{

    public function index(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'numeric|gt:0',
                'roomId' => 'numeric|gt:0',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $type = $request->input('type', 0);

        $user_id = UID;
        // 管理员获取房主的用户id
        $roomId = $request->input('roomId', 0);
        $roomInfo = app('model_user_room')->where('id', $roomId)->first();
        if (!$roomInfo) {
            throw new BasicException(10004, '房间信息不存在');
        }
        if ($roomInfo->user_id != $user_id) {
            // 判断是否为房间管理员
            $room_manager = app('model_room_manager')->where([
                'room_id' => $roomId,
                'user_id' => $user_id,
                'is_delete' => 0
            ])->first();
            if (empty($room_manager)) {
                throw new BasicException(10004, '非管理员不可操作');
            }
            $user_id = $roomInfo->user_id;
        }
        // 获取产品列表
        $isDynamic = RoomEnum::ROOM_TYPE_SINGLE == $roomInfo['sys_type_id'] ? 1 : 0;
        $result = app('repo_room_background')->index($user_id, $request->input(), $type, $isDynamic);

        return Response::sendData($result, '获取成功');
    }

    public function buy_background(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'filled|numeric',
                'price_id' => 'filled|numeric',
                'roomId' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $params = $request->only('id', 'price_id');
        $user_id = UID;
        $roomId = $request->input('roomId');
        // 判断是否为房间管理员
        $room_manager = app('model_room_manager')->where([
            'room_id' => $roomId,
            'user_id' => $user_id,
            'is_delete' => 0
        ])->first();
        if (!empty($room_manager)) {
            // 赠送流程
            $result = app('repo_room_background')->giveBackground($params, $user_id, $roomId);
        } else {
            $result = app('repo_room_background')->buyBackground($params, $user_id);
        }
        return $result;
    }

    public function use_background(Request $request)
    {
        $user_id = UID;
        // 获取id
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'filled|numeric',
                'roomId' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $params = $request->only('id', 'price', 'day', 'roomId');
        $roomInfo = app('model_user_room')->where('id', $params['roomId'])->first();
        if (!$roomInfo) {
            throw new BasicException(10004, '房间信息不存在');
        }
        if ($roomInfo->user_id != $user_id) {
            // 判断是否为房间管理员
            $room_manager = app('model_room_manager')->where([
                'room_id' => $params['roomId'],
                'user_id' => $user_id,
                'is_delete' => 0
            ])->first();
            if (empty($room_manager)) {
                throw new BasicException(10004, '非管理员不可操作');
            }
            $user_id = $roomInfo->user_id;
        }

        $backgroundInfo = app('model_room_background')
            ->with([
                'room_background_products_price' => function ($query) {
                    $query->select('id', 'room_background_id', 'price', 'day');
                },
            ])
            ->select('name', 'id', 'picture', 'effect_picture', 'is_free', 'special', 'is_dynamic')
            ->where('id', $params['id'])
            ->first();
        if (empty($backgroundInfo)) {
            throw new BasicException(401099);
        }
        $backgroundInfo = $backgroundInfo->toArray();
        if ($backgroundInfo['is_free'] == 1) {
            $expire_time = 86400 * 10;
        } else {
            $buy_log_info = app("model_room_background_buy_log")
                ->where('use_user_id', $user_id)
                ->where('room_background_id', $backgroundInfo['id'])
                ->where('overdue_time', '>', time())
                ->first();
            if (empty($buy_log_info)) {
                throw new BasicException(401100);
            }
            $expire_time = $buy_log_info->overdue_time - time();
        }

        app('repo_room_background')->setRoomBackground($backgroundInfo, $user_id, $params['roomId'], $expire_time);

        return Response::sendData(true);
    }

}
